/*
 * Copyright 2012-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;

/**
 * 过滤器，用于排除某些 Bean 定义，使其不被
 * {@link LazyInitializationBeanFactoryPostProcessor} 设置为
 * {@link AbstractBeanDefinition#setLazyInit(boolean) 延迟初始化}。
 * <p>
 * 主要用于支持下游项目处理难以支持延迟加载的边缘情况（例如动态创建额外 Bean 的 DSL）。
 * 将此过滤器实例添加到应用上下文中可解决这些特殊情况。
 * <p>
 * 典型示例：<pre>
 * &#64;Bean
 * public static LazyInitializationExcludeFilter integrationLazyInitializationExcludeFilter() {
 *   return LazyInitializationExcludeFilter.forBeanTypes(IntegrationFlow.class);
 * }
 * </pre>
 * <p>
 * 注意：此类型的 Bean 会在 Spring 应用生命周期的非常早期被实例化，
 * 因此通常应声明为 static 并且不应有任何依赖。
 *
 * @author Tyler Van Gorder
 * @author Philip Webb
 * @since 2.2.0
 */
@FunctionalInterface
public interface LazyInitializationExcludeFilter {

	/**
	 * 判断指定的 Bean 定义是否应排除自动设置延迟初始化。
	 * @param beanName Bean 名称
	 * @param beanDefinition Bean 定义
	 * @param beanType Bean 类型
	 * @return 如果不应自动设置延迟初始化则返回 {@code true}
	 */
	boolean isExcluded(String beanName, BeanDefinition beanDefinition, Class<?> beanType);

	/**
	 * 工厂方法，根据给定的 Bean 类型创建过滤器。
	 * @param types 过滤的类型
	 * @return 新的过滤器实例
	 */
	static LazyInitializationExcludeFilter forBeanTypes(Class<?>... types) {
		return (beanName, beanDefinition, beanType) -> {
			for (Class<?> type : types) {
				if (type.isAssignableFrom(beanType)) {
					return true;
				}
			}
			return false;
		};
	}

}
